home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1995 April / Internet Tools.iso / appletalk / netatalk / afs / afskrbsrc.sit.hqx / AFS Kerberos 1.0B0 / crypt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-08  |  8.4 KB  |  409 lines

  1. /* ============================================================================
  2.     This program implements the Proposed Federal Information Processing Data
  3.     Encryption Standard (DES). See Federal Register, March 17, 1975 (40FR12134)
  4.  
  5.     Portions of this code:
  6.         Copyright 1989 by the Massachusetts Institute of Technology
  7.     ============================================================================ */
  8.  
  9. /*
  10.  * Copyright (c) 1990 Regents of The University of Michigan.
  11.  * All Rights Reserved.
  12.  *
  13.  * Permission to use, copy, modify, and distribute this software
  14.  * and its documentation for any purpose and without fee is hereby
  15.  * granted, provided that the above copyright notice appears in all
  16.  * copies and that both that copyright notice and this permission
  17.  * notice appear in supporting documentation, and that the name of
  18.  * The University of Michigan not be used in advertising or
  19.  * publicity pertaining to distribution of the software without
  20.  * specific, written prior permission. This software is supplied as
  21.  * is without expressed or implied warranties of any kind.
  22.  *
  23.  *    ITD Research Systems
  24.  *    University of Michigan
  25.  *    535 W. William Street
  26.  *    Ann Arbor, Michigan
  27.  *    +1-313-936-2652
  28.  *    netatalk@terminator.cc.umich.edu
  29.  */
  30.  
  31. #include <Types.h>
  32.  
  33. /*
  34.  * Initial permutation,
  35.  */
  36. char    IP[] = {
  37.     58,50,42,34,26,18,10, 2,
  38.     60,52,44,36,28,20,12, 4,
  39.     62,54,46,38,30,22,14, 6,
  40.     64,56,48,40,32,24,16, 8,
  41.     57,49,41,33,25,17, 9, 1,
  42.     59,51,43,35,27,19,11, 3,
  43.     61,53,45,37,29,21,13, 5,
  44.     63,55,47,39,31,23,15, 7,
  45. };
  46.  
  47. /*
  48.  * Final permutation, FP = IP^(-1)
  49.  */
  50. char    FP[] = {
  51.     40, 8,48,16,56,24,64,32,
  52.     39, 7,47,15,55,23,63,31,
  53.     38, 6,46,14,54,22,62,30,
  54.     37, 5,45,13,53,21,61,29,
  55.     36, 4,44,12,52,20,60,28,
  56.     35, 3,43,11,51,19,59,27,
  57.     34, 2,42,10,50,18,58,26,
  58.     33, 1,41, 9,49,17,57,25,
  59. };
  60.  
  61. /*
  62.  * Permuted-choice 1 from the key bits to yield C and D.
  63.  * Note that bits 8,16... are left out: They are intended for a parity check.
  64.  */
  65. char    PC1_C[] = {
  66.     57,49,41,33,25,17, 9,
  67.      1,58,50,42,34,26,18,
  68.     10, 2,59,51,43,35,27,
  69.     19,11, 3,60,52,44,36,
  70. };
  71.  
  72. char    PC1_D[] = {
  73.     63,55,47,39,31,23,15,
  74.      7,62,54,46,38,30,22,
  75.     14, 6,61,53,45,37,29,
  76.     21,13, 5,28,20,12, 4,
  77. };
  78.  
  79. /*
  80.  * Sequence of shifts used for the key schedule.
  81.  */
  82. char    shifts[] = {
  83.     1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1,
  84. };
  85.  
  86. /*
  87.  * Permuted-choice 2, to pick out the bits from
  88.  * the CD array that generate the key schedule.
  89.  */
  90. char    PC2_C[] = {
  91.     14,17,11,24, 1, 5,
  92.      3,28,15, 6,21,10,
  93.     23,19,12, 4,26, 8,
  94.     16, 7,27,20,13, 2,
  95. };
  96.  
  97. char    PC2_D[] = {
  98.     41,52,31,37,47,55,
  99.     30,40,51,45,33,48,
  100.     44,49,39,56,34,53,
  101.     46,42,50,36,29,32,
  102. };
  103.  
  104. /*
  105.  * The E bit-selection table.
  106.  */
  107. char    E[48];
  108. char    e[] = {
  109.     32, 1, 2, 3, 4, 5,
  110.      4, 5, 6, 7, 8, 9,
  111.      8, 9,10,11,12,13,
  112.     12,13,14,15,16,17,
  113.     16,17,18,19,20,21,
  114.     20,21,22,23,24,25,
  115.     24,25,26,27,28,29,
  116.     28,29,30,31,32, 1,
  117. };
  118.  
  119. /*
  120.  * P is a permutation on the selected combination
  121.  * of the current L and key.
  122.  */
  123. char    P[] = {
  124.     16, 7,20,21,
  125.     29,12,28,17,
  126.      1,15,23,26,
  127.      5,18,31,10,
  128.      2, 8,24,14,
  129.     32,27, 3, 9,
  130.     19,13,30, 6,
  131.     22,11, 4,25,
  132. };
  133.  
  134. /*
  135.  * The 8 selection functions.
  136.  * For some reason, they give a 0-origin
  137.  * index, unlike everything else.
  138.  */
  139. char    S[8][64] = {
  140.     14, 4,13, 1, 2,15,11, 8, 3,10, 6,12, 5, 9, 0, 7,
  141.      0,15, 7, 4,14, 2,13, 1,10, 6,12,11, 9, 5, 3, 8,
  142.      4, 1,14, 8,13, 6, 2,11,15,12, 9, 7, 3,10, 5, 0,
  143.     15,12, 8, 2, 4, 9, 1, 7, 5,11, 3,14,10, 0, 6,13,
  144.  
  145.     15, 1, 8,14, 6,11, 3, 4, 9, 7, 2,13,12, 0, 5,10,
  146.      3,13, 4, 7,15, 2, 8,14,12, 0, 1,10, 6, 9,11, 5,
  147.      0,14, 7,11,10, 4,13, 1, 5, 8,12, 6, 9, 3, 2,15,
  148.     13, 8,10, 1, 3,15, 4, 2,11, 6, 7,12, 0, 5,14, 9,
  149.  
  150.     10, 0, 9,14, 6, 3,15, 5, 1,13,12, 7,11, 4, 2, 8,
  151.     13, 7, 0, 9, 3, 4, 6,10, 2, 8, 5,14,12,11,15, 1,
  152.     13, 6, 4, 9, 8,15, 3, 0,11, 1, 2,12, 5,10,14, 7,
  153.      1,10,13, 0, 6, 9, 8, 7, 4,15,14, 3,11, 5, 2,12,
  154.  
  155.      7,13,14, 3, 0, 6, 9,10, 1, 2, 8, 5,11,12, 4,15,
  156.     13, 8,11, 5, 6,15, 0, 3, 4, 7, 2,12, 1,10,14, 9,
  157.     10, 6, 9, 0,12,11, 7,13,15, 1, 3,14, 5, 2, 8, 4,
  158.      3,15, 0, 6,10, 1,13, 8, 9, 4, 5,11,12, 7, 2,14,
  159.  
  160.      2,12, 4, 1, 7,10,11, 6, 8, 5, 3,15,13, 0,14, 9,
  161.     14,11, 2,12, 4, 7,13, 1, 5, 0,15,10, 3, 9, 8, 6,
  162.      4, 2, 1,11,10,13, 7, 8,15, 9,12, 5, 6, 3, 0,14,
  163.     11, 8,12, 7, 1,14, 2,13, 6,15, 0, 9,10, 4, 5, 3,
  164.  
  165.     12, 1,10,15, 9, 2, 6, 8, 0,13, 3, 4,14, 7, 5,11,
  166.     10,15, 4, 2, 7,12, 9, 5, 6, 1,13,14, 0,11, 3, 8,
  167.      9,14,15, 5, 2, 8,12, 3, 7, 0, 4,10, 1,13,11, 6,
  168.      4, 3, 2,12, 9, 5,15,10,11,14, 1, 7, 6, 0, 8,13,
  169.  
  170.      4,11, 2,14,15, 0, 8,13, 3,12, 9, 7, 5,10, 6, 1,
  171.     13, 0,11, 7, 4, 9, 1,10,14, 3, 5,12, 2,15, 8, 6,
  172.      1, 4,11,13,12, 3, 7,14,10,15, 6, 8, 0, 5, 9, 2,
  173.      6,11,13, 8, 1, 4,10, 7, 9, 5, 0,15,14, 2, 3,12,
  174.  
  175.     13, 2, 8, 4, 6,15,11, 1,10, 9, 3,14, 5, 0,12, 7,
  176.      1,15,13, 8,10, 3, 7, 4,12, 5, 6,11, 0,14, 9, 2,
  177.      7,11, 4, 1, 9,12,14, 2, 0, 6,10,13,15, 3, 5, 8,
  178.      2, 1,14, 7, 4,10, 8,13,15,12, 9, 0, 3, 5, 6,11,
  179. };
  180.  
  181. /*
  182.  * The C and D arrays used to calculate the key schedule.
  183.  */
  184.  
  185. char    C[28];
  186. char    D[28];
  187. /*
  188.  * The key schedule.
  189.  * Generated from the key.
  190.  */
  191. char    KS[16][48];
  192.  
  193. /*
  194.  * The current block, divided into 2 halves.
  195.  */
  196. #ifdef macintosh
  197. char    R[32], L[32];
  198. #else
  199. char    L[32], R[32];
  200. #endif
  201. char    tempL[32];
  202. char    f[32];
  203.  
  204. /*
  205.  * The combination of the key and the input, before selection.
  206.  */
  207. char    preS[48];
  208.  
  209. char *crypt(pw, salt)
  210. char *pw;
  211. char *salt;
  212. {
  213.     register i, j, c;
  214.     int temp;
  215.     static char block[66], iobuf[16];
  216.  
  217.     for(i=0; i<66; i++)
  218.         block[i] = 0;
  219.     for(i=0; (c= *pw) && i<64; pw++){
  220.         for(j=0; j<7; j++, i++)
  221.             block[i] = (c>>(6-j)) & 01;
  222.         i++;
  223.     }
  224.     
  225.     setkey(block);
  226.     
  227.     for(i=0; i<66; i++)
  228.         block[i] = 0;
  229.  
  230.     for(i=0;i<2;i++){
  231.         c = *salt++;
  232.         iobuf[i] = c;
  233.         if(c>'Z') c -= 6;
  234.         if(c>'9') c -= 7;
  235.         c -= '.';
  236.         for(j=0;j<6;j++){
  237.             if((c>>j) & 01){
  238.                 temp = E[6*i+j];
  239.                 E[6*i+j] = E[6*i+j+24];
  240.                 E[6*i+j+24] = temp;
  241.                 }
  242.             }
  243.         }
  244.     
  245.     for(i=0; i<25; i++)
  246.         encrypt(block,0);
  247.     
  248.     for(i=0; i<11; i++){
  249.         c = 0;
  250.         for(j=0; j<6; j++){
  251.             c <<= 1;
  252.             c |= block[6*i+j];
  253.             }
  254.         c += '.';
  255.         if(c>'9') c += 7;
  256.         if(c>'Z') c += 6;
  257.         iobuf[i+2] = c;
  258.     }
  259.     iobuf[i+2] = 0;
  260.     if(iobuf[1]==0)
  261.         iobuf[1] = iobuf[0];
  262.     return(iobuf);
  263. }
  264.  
  265. /*
  266.  * Set up the key schedule from the key.
  267.  */
  268.  
  269. setkey(key)
  270. char *key;
  271. {
  272.     register i, j, k;
  273.     int t;
  274.  
  275.     /*
  276.      * First, generate C and D by permuting
  277.      * the key.  The low order bit of each
  278.      * 8-bit char is not used, so C and D are only 28
  279.      * bits apiece.
  280.      */
  281.     for (i=0; i<28; i++) {
  282.         C[i] = key[PC1_C[i]-1];
  283.         D[i] = key[PC1_D[i]-1];
  284.     }
  285.     /*
  286.      * To generate Ki, rotate C and D according
  287.      * to schedule and pick up a permutation
  288.      * using PC2.
  289.      */
  290.     for (i=0; i<16; i++) {
  291.         /*
  292.          * rotate.
  293.          */
  294.         for (k=0; k<shifts[i]; k++) {
  295.             t = C[0];
  296.             for (j=0; j<28-1; j++)
  297.                 C[j] = C[j+1];
  298.             C[27] = t;
  299.             t = D[0];
  300.             for (j=0; j<28-1; j++)
  301.                 D[j] = D[j+1];
  302.             D[27] = t;
  303.         }
  304.         /*
  305.          * get Ki. Note C and D are concatenated.
  306.          */
  307.         for (j=0; j<24; j++) {
  308.             KS[i][j] = C[PC2_C[j]-1];
  309.             KS[i][j+24] = D[PC2_D[j]-28-1];
  310.         }
  311.     }
  312.  
  313.     for(i=0;i<48;i++) {
  314.         E[i] = e[i];
  315.     }
  316. }
  317.  
  318. /*
  319.  * The payoff: encrypt a block.
  320.  */
  321.  
  322. encrypt(block, edflag)
  323. char *block;
  324. {
  325.     int i, ii;
  326.     register t, j, k;
  327.  
  328.     /*
  329.      * First, permute the bits in the input
  330.      */
  331.     for (j=0; j<64; j++)
  332.         L[j] = block[IP[j]-1];
  333.     /*
  334.      * Perform an encryption operation 16 times.
  335.      */
  336.     for (ii=0; ii<16; ii++) {
  337.         /*
  338.          * Set direction
  339.          */
  340.         if (edflag)
  341.             i = 15-ii;
  342.         else
  343.             i = ii;
  344.         /*
  345.          * Save the R array,
  346.          * which will be the new L.
  347.          */
  348.         for (j=0; j<32; j++)
  349.             tempL[j] = R[j];
  350.         /*
  351.          * Expand R to 48 bits using the E selector;
  352.          * exclusive-or with the current key bits.
  353.          */
  354.         for (j=0; j<48; j++)
  355.             preS[j] = R[E[j]-1] ^ KS[i][j];
  356.         /*
  357.          * The pre-select bits are now considered
  358.          * in 8 groups of 6 bits each.
  359.          * The 8 selection functions map these
  360.          * 6-bit quantities into 4-bit quantities
  361.          * and the results permuted
  362.          * to make an f(R, K).
  363.          * The indexing into the selection functions
  364.          * is peculiar; it could be simplified by
  365.          * rewriting the tables.
  366.          */
  367.         for (j=0; j<8; j++) {
  368.             t = 6*j;
  369.             k = S[j][(preS[t+0]<<5)+
  370.                 (preS[t+1]<<3)+
  371.                 (preS[t+2]<<2)+
  372.                 (preS[t+3]<<1)+
  373.                 (preS[t+4]<<0)+
  374.                 (preS[t+5]<<4)];
  375.             t = 4*j;
  376.                 f[t+0] = (k>>3)&01;
  377.                 f[t+1] = (k>>2)&01;
  378.                 f[t+2] = (k>>1)&01;
  379.                 f[t+3] = (k>>0)&01;
  380.         }
  381.         /*
  382.          * The new R is L ^ f(R, K).
  383.          * The f here has to be permuted first, though.
  384.          */
  385.         for (j=0; j<32; j++)
  386.             R[j] = L[j] ^ f[P[j]-1];
  387.         /*
  388.          * Finally, the new L (the original R)
  389.          * is copied back.
  390.          */
  391.         for (j=0; j<32; j++)
  392.             L[j] = tempL[j];
  393.     }
  394.     /*
  395.      * The output L and R are reversed.
  396.      */
  397.     for (j=0; j<32; j++) {
  398.         t = L[j];
  399.         L[j] = R[j];
  400.         R[j] = t;
  401.     }
  402.     /*
  403.      * The final output
  404.      * gets the inverse permutation of the very original.
  405.      */
  406.     for (j=0; j<64; j++)
  407.         block[j] = L[FP[j]-1];
  408. }
  409.